# 混合型排行前50

# Request URL:'http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=hh&rs=&gs=0&sc=qjzf&st=desc&sd=2021-04-16&ed=2022-04-16&qdii=&tabSubtype=,,,,,&pi=1&pn=50&dx=1&v=0.9022380260304017'

import re
import pandas
import requests
import csv
import sqlite3

def HunHeRanking():
    # 伪装请求头
    headers = {
        # ctrl+r    (.*)':'(.*)替换'$1':'$2'
        'Cookie':'qgqp_b_id=e1d7a84f522c1ca42156044d65056541; EMFUND1=null; EMFUND2=null; Eastmoney_Fund=; kforders=0%3B-1%3B%3B%3B0%2C2%2C24%2C25%2C18%2C19%2C22%2C23%2C21%2C3; ASP.NET_SessionId=fryj3eq4tj0cvo1jtdusqyyj; st_si=25789683058686; st_asi=delete; EMFUND0=null; EMFUND3=04-12%2022%3A54%3A06@%23%24%u5E7F%u53D1%u9053%u743C%u65AF%u77F3%u6CB9%u6307%u6570%u4EBA%u6C11%u5E01C@%23%24004243; EMFUND4=04-12%2022%3A57%3A16@%23%24%u6CF0%u4FE1%u884C%u4E1A%u7CBE%u9009%u6DF7%u5408A@%23%24290012; EMFUND5=04-14%2000%3A01%3A45@%23%24%u5E7F%u53D1%u9053%u743C%u65AF%u77F3%u6CB9%u6307%u6570%u4EBA%u6C11%u5E01A@%23%24162719; EMFUND6=04-13%2023%3A46%3A02@%23%24%u4E07%u5BB6%u7CBE%u9009%u6DF7%u5408@%23%24519185; EMFUND7=04-15%2015%3A43%3A35@%23%24%u534E%u590F%u6210%u957F%u6DF7%u5408@%23%24000001; EMFUND8=04-15%2021%3A05%3A27@%23%24%u4E07%u5BB6%u5B8F%u89C2%u62E9%u65F6%u591A%u7B56%u7565%u6DF7%u5408@%23%24519212; _adsame_fullscreen_18503=1; EMFUND9=04-16 19:39:28@#$%u9E4F%u534E%u4E2D%u8BC1800%u5730%u4EA7%u6307%u6570%28LOF%29@%23%24160628; st_pvi=89163971024888; st_sp=2022-04-12%2016%3A24%3A17; st_inirUrl=http%3A%2F%2Ffundf10.eastmoney.com%2F; st_sn=12; st_psi=20220416195141490-112200312936-0894237276',
        'Host':'fund.eastmoney.com',
        'Referer':'http://fund.eastmoney.com/data/fundranking.html',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36'
    }
    for page in range(1,2):
        print(f'------------------正在爬取股票型排行第{page}页内容')
        url  = f'http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=hh&rs=&gs=0&sc=qjzf&st=desc&sd=2021-04-16&ed=2022-04-16&qdii=&tabSubtype=,,,,,&pi={page}&pn=50&dx=1&v=0.9022380260304017'



        # 1. 发送请求
        # <Response [200]>:发送请求成功结果
        response = requests.get(url=url,headers=headers)

        # 2. 获取数据
        data = response.text

        # 3. 解析数据 筛选数据
        # 正则re
        data_str = re.findall('\[(.*?)\]',data)[0]
        # 4. 保存数据 表格/数据库
        tuple_data = eval(data_str)     #字符串分割


        for td in tuple_data:
            td_list = td.split(',')
            with open('./ranking_csv/混合型排行.csv',mode='a',encoding='utf-8',newline='') as f:
                csv_write = csv.writer(f)
                csv_write.writerow(td_list)
            print(td)

    # csv写入数据库
    conn = sqlite3.connect("fundDB.db")
    df = pandas.read_csv('./ranking_csv/混合型排行.csv', header=None,
                             names=['基金代码', '基金名称','基金简称', '日期', '单位净值', '累计净值', '日增长率', '近1周', '近1月', '近3月', '近6月', '近1年', '近2年',
                                    '近3年', '今年来', '成立来', '成立时间', 'data1', '总体', 'data2', '手续费', 'data3', 'data4','data5','data6'])
    df = df.loc[:,
         ['基金代码', '基金名称', '日期', '单位净值', '累计净值', '日增长率', '近1周', '近1月', '近3月', '近6月', '近1年', '成立时间', '手续费']]
    df.to_csv('./ranking_csv/混合型排行.csv', index=False)
    df.to_sql('HunHeRanking', conn, if_exists='append', index=False)
    conn.close()
